/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/2/15
 */
import React, {Component, Fragment} from 'react';
import SimpleTable from "../../../commons/components/SimpleTable";
import ToolBar from "../../../commons/components/toolbar/ToolBar";
import {searchListByKey} from "../../../commons/utils/CommonUtils";

class ShowTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataSource: [],
            searchData: [],
            searchValue: "",
            loading: false
        }
    }
    componentDidMount(){
        this.getDataSource();
    }
    getDataSource = () => {
        this.setState({loading: true});
        const {dataService} = this.props;
        dataService && dataService(this.props.id).then((res)=>{
            this.setState({dataSource:res});
        }).catch(e => {
        }).finally(() => {
            this.setState({loading: false});
        });
    }

    searchBtnCfg = () => {
        return {
            quickSearchCfg: {
                style: {width: 250},
                placeholder: "输入用户名称或用户类型查询",
                onSearch: (v) => {
                    this.handleQuickSearch(v)
                }
            }
        }
    }
    handleQuickSearch = (searchValue) => {
        const {quickSearchKey=['code', 'name']} = this.props;
        searchListByKey(this.state.dataSource, {keyword: searchValue}, quickSearchKey)
            .then(searchData => {this.setState({searchData, searchValue})
        })
    }
    render() {
        const {dataSource,searchValue,searchData,loading} = this.state;
        const {columns} = this.props;
        return (
            <Fragment>
                <ToolBar
                    searchBtnCfg={this.searchBtnCfg()}
                />
                <SimpleTable
                    // rowsSelected={this.state.selectedRows}
                    loading={loading}
                    data={searchValue ? searchData : dataSource}
                    columns={columns}
                />
            </Fragment>

        );
    }
}
export default ShowTable;
